﻿//Test by Bangle
using System; 
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic; 
using System.Data;
using Bangle.DBUtility;
namespace Bangle.DAL  
{
	 	//T_Sale
		public partial class T_SaleDAL
	{
   		     
		public bool Exists(Guid SaleId)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from T_Sale");
			strSql.Append(" where ");
			                                       strSql.Append(" SaleId = @SaleId  ");
                            			SqlParameter[] parameters = {
					new SqlParameter("@SaleId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = SaleId;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}
		
				
		
		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(Bangle.Model.T_Sale model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into T_Sale(");			
            strSql.Append("SaleId,SaleCode,SaleTime,CustomerId,Salesman,ExchangeRate,Freight,PayFreight,Payment,StateId,Description,CreateUserId,CreateTime,UpdateUserId,UpdateTime,Valid");
			strSql.Append(") values (");
            strSql.Append("@SaleId,@SaleCode,@SaleTime,@CustomerId,@Salesman,@ExchangeRate,@Freight,@PayFreight,@Payment,@StateId,@Description,@CreateUserId,@CreateTime,@UpdateUserId,@UpdateTime,@Valid");            
            strSql.Append(") ");            
            		
			SqlParameter[] parameters = {
			            new SqlParameter("@SaleId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SaleCode", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@SaleTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CustomerId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@Salesman", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@ExchangeRate", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Freight", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayFreight", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Payment", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@StateId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Description", SqlDbType.VarChar,5000) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };
			            
            parameters[0].Value = model.SaleId;                        
            parameters[1].Value = model.SaleCode;                        
            parameters[2].Value = model.SaleTime;                        
            parameters[3].Value = model.CustomerId;                        
            parameters[4].Value = model.Salesman;                        
            parameters[5].Value = model.ExchangeRate;                        
            parameters[6].Value = model.Freight;                        
            parameters[7].Value = model.PayFreight;                        
            parameters[8].Value = model.Payment;                        
            parameters[9].Value = model.StateId;                        
            parameters[10].Value = model.Description;                        
            parameters[11].Value = model.CreateUserId;                        
            parameters[12].Value = model.CreateTime;                        
            parameters[13].Value = model.UpdateUserId;                        
            parameters[14].Value = model.UpdateTime;                        
            parameters[15].Value = model.Valid;                        
			            DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
            			
		}
		
		
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Bangle.Model.T_Sale model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update T_Sale set ");
			                        
            strSql.Append(" SaleId = @SaleId , ");                                    
            strSql.Append(" SaleCode = @SaleCode , ");                                    
            strSql.Append(" SaleTime = @SaleTime , ");                                    
            strSql.Append(" CustomerId = @CustomerId , ");                                    
            strSql.Append(" Salesman = @Salesman , ");                                    
            strSql.Append(" ExchangeRate = @ExchangeRate , ");                                    
            strSql.Append(" Freight = @Freight , ");                                    
            strSql.Append(" PayFreight = @PayFreight , ");                                    
            strSql.Append(" Payment = @Payment , ");                                    
            strSql.Append(" StateId = @StateId , ");                                    
            strSql.Append(" Description = @Description , ");                                    
            strSql.Append(" CreateUserId = @CreateUserId , ");                                    
            strSql.Append(" CreateTime = @CreateTime , ");                                    
            strSql.Append(" UpdateUserId = @UpdateUserId , ");                                    
            strSql.Append(" UpdateTime = @UpdateTime , ");                                    
            strSql.Append(" Valid = @Valid  ");            			
			strSql.Append(" where SaleId=@SaleId  ");
						
SqlParameter[] parameters = {
			            new SqlParameter("@SaleId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@SaleCode", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@SaleTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@CustomerId", SqlDbType.UniqueIdentifier,16) ,            
                        new SqlParameter("@Salesman", SqlDbType.VarChar,50) ,            
                        new SqlParameter("@ExchangeRate", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Freight", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@PayFreight", SqlDbType.Decimal,9) ,            
                        new SqlParameter("@Payment", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@StateId", SqlDbType.Int,4) ,            
                        new SqlParameter("@Description", SqlDbType.VarChar,5000) ,            
                        new SqlParameter("@CreateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@CreateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@UpdateUserId", SqlDbType.Int,4) ,            
                        new SqlParameter("@UpdateTime", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Valid", SqlDbType.Int,4)             
              
            };
						            
            parameters[0].Value = model.SaleId;                        
            parameters[1].Value = model.SaleCode;                        
            parameters[2].Value = model.SaleTime;                        
            parameters[3].Value = model.CustomerId;                        
            parameters[4].Value = model.Salesman;                        
            parameters[5].Value = model.ExchangeRate;                        
            parameters[6].Value = model.Freight;                        
            parameters[7].Value = model.PayFreight;                        
            parameters[8].Value = model.Payment;                        
            parameters[9].Value = model.StateId;                        
            parameters[10].Value = model.Description;                        
            parameters[11].Value = model.CreateUserId;                        
            parameters[12].Value = model.CreateTime;                        
            parameters[13].Value = model.UpdateUserId;                        
            parameters[14].Value = model.UpdateTime;                        
            parameters[15].Value = model.Valid;                        
            int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(Guid SaleId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from T_Sale ");
			strSql.Append(" where SaleId=@SaleId ");
						SqlParameter[] parameters = {
					new SqlParameter("@SaleId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = SaleId;


			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
				
		
		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Bangle.Model.T_Sale GetModel(Guid SaleId)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SaleId, SaleCode, SaleTime, CustomerId, Salesman, ExchangeRate, Freight, PayFreight, Payment, StateId, Description, CreateUserId, CreateTime, UpdateUserId, UpdateTime, Valid  ");			
			strSql.Append("  from T_Sale ");
			strSql.Append(" where SaleId=@SaleId ");
						SqlParameter[] parameters = {
					new SqlParameter("@SaleId", SqlDbType.UniqueIdentifier,16)			};
			parameters[0].Value = SaleId;

			
			Bangle.Model.T_Sale model=new Bangle.Model.T_Sale();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			
			if(ds.Tables[0].Rows.Count>0)
			{
																								if(ds.Tables[0].Rows[0]["SaleId"].ToString()!="")
				{
					model.SaleId= Guid.Parse(ds.Tables[0].Rows[0]["SaleId"].ToString());
				}
																								model.SaleCode= ds.Tables[0].Rows[0]["SaleCode"].ToString();
																																model.SaleTime= ds.Tables[0].Rows[0]["SaleTime"].ToString();
																																								if(ds.Tables[0].Rows[0]["CustomerId"].ToString()!="")
				{
					model.CustomerId= Guid.Parse(ds.Tables[0].Rows[0]["CustomerId"].ToString());
				}
																								model.Salesman= ds.Tables[0].Rows[0]["Salesman"].ToString();
																												if(ds.Tables[0].Rows[0]["ExchangeRate"].ToString()!="")
				{
					model.ExchangeRate=decimal.Parse(ds.Tables[0].Rows[0]["ExchangeRate"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["Freight"].ToString()!="")
				{
					model.Freight=decimal.Parse(ds.Tables[0].Rows[0]["Freight"].ToString());
				}
																																if(ds.Tables[0].Rows[0]["PayFreight"].ToString()!="")
				{
					model.PayFreight=decimal.Parse(ds.Tables[0].Rows[0]["PayFreight"].ToString());
				}
																																				model.Payment= ds.Tables[0].Rows[0]["Payment"].ToString();
																												if(ds.Tables[0].Rows[0]["StateId"].ToString()!="")
				{
					model.StateId=int.Parse(ds.Tables[0].Rows[0]["StateId"].ToString());
				}
																																				model.Description= ds.Tables[0].Rows[0]["Description"].ToString();
																												if(ds.Tables[0].Rows[0]["CreateUserId"].ToString()!="")
				{
					model.CreateUserId=int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
				}
																																				model.CreateTime= ds.Tables[0].Rows[0]["CreateTime"].ToString();
																												if(ds.Tables[0].Rows[0]["UpdateUserId"].ToString()!="")
				{
					model.UpdateUserId=int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
				}
																																				model.UpdateTime= ds.Tables[0].Rows[0]["UpdateTime"].ToString();
																												if(ds.Tables[0].Rows[0]["Valid"].ToString()!="")
				{
					model.Valid=int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
				}
																														
				return model;
			}
			else
			{
				return null;
			}
		}
		
		
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select * ");
			strSql.Append(" FROM T_Sale ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}
		
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" * ");
			strSql.Append(" FROM T_Sale ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

   
	}
}

